package com.ops.service.ns.system.exceptionhandler;

import com.ops.service.ns.system.exception.OPSException;
import com.ops.service.ns.system.constant.OPSConstant;
import org.springframework.dao.DataAccessException;
import com.ops.schema.ns.system.System;
import com.ops.schema.ns.system.ErrorType;
import com.ops.schema.ns.system.FaultParameters;
import com.ops.service.ns.system.FaultMessage;

public final class ExceptionHandler {
    
    private ExceptionHandler()
    {
        
    }
    
    public static OPSException generateOPSException(Exception ex, String code) {
        OPSException exception = null;
        String errorCode = "";
        if(ex instanceof DataAccessException) {
            if(code != null && !code.equals("")) {
                errorCode = code;
            } else {
                errorCode = OPSConstant.geterror_CODE_DATAACCESS_ERROR_GENERIC();
            }
            exception =  new OPSException(code,
                    OPSConstant.getsystem_DATABASE(),
                    OPSConstant.geterror_TYPE_TECHNICAL(),
                    OPSConstant.geterror_CODE_DATAACCESS_ERROR_DESC() + ex.getMessage());
        } else {
            if(code != null && !code.equals("")) {
                errorCode = code;
            } else {
                errorCode = OPSConstant.geterror_CODE_WAS_ERROR_GENERIC();
            }
            exception =  new OPSException(errorCode,
                    OPSConstant.getsystem_WAS(),
                    OPSConstant.geterror_TYPE_TECHNICAL(),
                    OPSConstant.geterror_CODE_WAS_ERROR_DESC() + ex.getMessage());
        }
        return exception;
    }
    
    public static OPSException generateDBConnectionOPSException() {
        OPSException exception = new OPSException(OPSConstant.geterror_CODE_DATAACCESS_ERROR_GENERIC(),
                OPSConstant.getsystem_DATABASE(),
                OPSConstant.geterror_TYPE_TECHNICAL(),
                OPSConstant.geterror_CODE_DATAACCESS_ERROR_DESC() + OPSConstant.geterror_CODE_DATAACCESS_CONNECTION_ERROR_DESC());
        return exception;
    }    
    

    public static FaultMessage generateFaultMessage(OPSException ex) {
        
        System system = null;
        if(ex != null && ex.getSystem() != null && ex.getSystem().equals(OPSConstant.getsystem_DATAPOWER())) {
            system = System.fromValue(OPSConstant.getsystem_DATAPOWER());
        }else if(ex != null && ex.getSystem() != null && ex.getSystem().equals(OPSConstant.getsystem_WAS())) {
            system = System.fromValue(OPSConstant.getsystem_WAS());
        }else if(ex != null && ex.getSystem() != null && ex.getSystem().equals(OPSConstant.getsystem_DATABASE())) {
            system = System.fromValue(OPSConstant.getsystem_DATABASE());
        }
        ErrorType errorType = null;
        if(ex != null && ex.getType() != null && ex.getType().equals(OPSConstant.geterror_TYPE_TECHNICAL())) {
            errorType = ErrorType.fromValue(OPSConstant.geterror_TYPE_TECHNICAL());
        }else if(ex != null && ex.getType() != null && ex.getType().equals(OPSConstant.geterror_TYPE_BUSINESS())) {
            errorType = ErrorType.fromValue(OPSConstant.geterror_TYPE_BUSINESS());
        }
        FaultParameters parameter = new FaultParameters();
        parameter.setCode(ex.getCode());
        parameter.setSystem(system);
        parameter.setType(errorType);
        parameter.setDescription(ex.getDescription());
        
        FaultMessage exception = new FaultMessage(ex.getDescription(), parameter);
        
        return exception;
    }

    public static FaultMessage generateFaultMessage(Exception ex) {
        
        System system = System.fromValue(OPSConstant.getsystem_WAS());
        ErrorType errorType = ErrorType.fromValue(OPSConstant.geterror_TYPE_TECHNICAL());
        FaultParameters parameter = new FaultParameters();
        parameter.setCode(OPSConstant.geterror_CODE_WAS_ERROR_GENERIC());
        parameter.setSystem(system);
        parameter.setType(errorType);
        parameter.setDescription(OPSConstant.geterror_CODE_WAS_ERROR_DESC() + ex.getMessage());
        
        FaultMessage exception = new FaultMessage(OPSConstant.geterror_CODE_WAS_ERROR_DESC() + ex.getMessage(), parameter);
        
        return exception;
    }
    
}
